﻿using MyDay13.Context;
using MyDay13.IRepostitype;
using MyDay13.Models;
using MyDay3.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyDay13Repostitype
{
    public class TreeRepostitypr : ITreeRepostitype
    {
        Day13Context db;
        public TreeRepostitypr(Day13Context dbs)
        {
            db = dbs;
        }
        public List<MenoTo> MenoShow(int pid)
        {
            var list = db.D13Meno.ToList();
            return MenoGui(list,pid);
        }
        private List<MenoTo> MenoGui(List<D13Meno> d13Menos,int pid)
        {
            var list = db.D13Meno.ToList().Where(x => x.PId == pid).ToList();
            List<MenoTo> d13Menos1 = new List<MenoTo>();
            foreach (var item in list)
            {
                d13Menos1.Add(new MenoTo()
                {
                    MId = item.MId.ToString(),
                    AddrUrl = item.AddrUrl,
                    MName = item.MName,
                    PId = item.PId,
                    menoTos = MenoGui(d13Menos,item.MId)
                });
            }
            return d13Menos1;
        }
        public List<PrwerTo> PrwerShow(int pid)
        {
            var list = db.D13Power.ToList();
            return PrwerGui(list,pid);
        }
        private List<PrwerTo> PrwerGui(List<D13Power> d13Powers,int pid)
        {
            var list = db.D13Power.ToList().Where(x => x.PIdNumber == pid).ToList();
            List<PrwerTo> prwerTos = new List<PrwerTo>();
            foreach (var item in list)
            {
                prwerTos.Add(new PrwerTo()
                {
                    PId = item.PId.ToString(),
                    PIdNumber = item.PIdNumber,
                    PowerName = item.PowerName,
                    PUrl=item.PUrl,
                    prwerTos = PrwerGui(d13Powers, item.PId)
                });
            }
            return prwerTos;
        }

        public List<ViewRP> RPShow()
        {
            var list = (from r in db.D13Role
                        join rp in db.D13RolePowerTable on r.RId equals rp.RoleId
                        join p in db.D13Power on rp.PowerId equals p.PId
                        select new ViewRP()
                        {
                             RPId=rp.RPId,
                            PId = p.PId,
                            PIdNumber = p.PIdNumber,
                            PowerName = p.PowerName,
                            RId = r.RId,
                            RoleName = r.RoleName,
                            RoleText = r.RoleText,
                             PUrl=p.PUrl
                        }).ToList();
            return list;
        }

        public List<ViewUR> URShow()
        {
            var list = (from u in db.D13User
                        join ur in db.D13UserRoleTable on u.UId equals ur.UserId
                        join r in db.D13Role on ur.RoleId equals r.RId
                        select new ViewUR()
                        {
                            RId = r.RId,
                            Account = u.Account,
                            Phone = u.Phone,
                            Pwd = u.Pwd,
                            RoleName = r.RoleName,
                            RoleText = r.RoleText,
                            stata = u.stata,
                            UId = u.UId,
                            UserName = u.UserName
                        }).ToList();
            return list;
        }

        public int RPdistribution(int rid, string pid)
        {
            var list = db.D13RolePowerTable.ToList().Where(x => x.RoleId == rid).ToList();
            if (list!=null)
            {
                db.D13RolePowerTable.RemoveRange(list);
            }
            List<D13RolePowerTable> rplist = new List<D13RolePowerTable>();
            var spid = pid.Split(',');
            spid.ToList().ForEach(x =>
            {
                rplist.Add(new D13RolePowerTable() { PowerId = Convert.ToInt32(x), RoleId = rid });
            });
            db.D13RolePowerTable.AddRange(rplist);
            return db.SaveChanges();
        }

        public int URdistribution(int uid, string rid)
        {
            var list = db.D13UserRoleTable.ToList().Where(x => x.UserId == uid).ToList();
            if (list!=null)
            {
                db.D13UserRoleTable.RemoveRange(list);
            }
            var srid = rid.Split(',');
            List<D13UserRoleTable> urlist = new List<D13UserRoleTable>();
            srid.ToList().ForEach(x =>
            {
                urlist.Add(new D13UserRoleTable() { RoleId = Convert.ToInt32(x), UserId = uid });
            });
            db.D13UserRoleTable.AddRange(urlist);
            return db.SaveChanges();
        }

        public int Login(string name, string pwd)
        {
            var flag = 0;
            var list = db.D13User.ToList().Where(x => x.Account == name & x.Pwd == pwd).FirstOrDefault();
            if (list!=null)
            {
                flag = list.UId;
            }
            return flag;
        }

        public List<PrwerTo> URPShow(int id)
        {
            
            var list = (from u in db.D13User
                        join ur in db.D13UserRoleTable on u.UId equals ur.UserId
                        join r in db.D13Role on ur.RoleId equals r.RId
                        join rp in db.D13RolePowerTable on r.RId equals rp.RoleId
                        join p in db.D13Power on rp.PowerId equals p.PId where u.UId==id
                        select p).ToList();

            return LDIGUI(list,0);
        }
       private List<PrwerTo> LDIGUI(List<D13Power> d13Powers,int pid)
        {
            var list = db.D13Power.ToList().Where(x => x.PIdNumber == pid).ToList();
            List<PrwerTo> str = new List<PrwerTo>();
            foreach (var item in list)
            {
                str.Add(new PrwerTo()
                {
                    PId = item.PId.ToString(),
                    PIdNumber = item.PIdNumber,
                    PowerName = item.PowerName,
                    PUrl = item.PUrl,
                    prwerTos = LDIGUI(d13Powers,item.PId)
                });
            }
            return str;
        }
    }
}
